/**
 * @description 任务服务类
 * @author AI Assistant
 * @date 2023-10-27
 */

import { BaseService } from './base'
import { STORAGE_KEYS, storage } from '@/utils/storage'
import { Task } from '@/models/task'

export class TaskService extends BaseService {
  constructor() {
    super(STORAGE_KEYS.TASKS)
  }

  create(data) {
    const task = new Task(data)
    return super.create(task.toJSON())
  }

  // 根据分组ID获取任务列表
  getTasksByGroupId(groupId) {
    const tasks = this.getAll()
    return tasks.filter(task => task.groupId === groupId)
      .sort((a, b) => a.order - b.order)
  }

  // 更新任务状态
  updateStatus(id, status) {
    return this.update(id, { status, updateTime: Date.now() })
  }

  // 更新任务排序
  updateOrder(taskIds) {
    const tasks = this.getAll()
    let updated = false

    taskIds.forEach((id, index) => {
      const taskIndex = tasks.findIndex(task => task.id === id)
      if (taskIndex > -1) {
        tasks[taskIndex].order = index
        tasks[taskIndex].updateTime = Date.now()
        updated = true
      }
    })

    if (updated) {
      storage.set(this.storageKey, tasks)
    }
    return updated
  }
  
  // 获取待办任务
  getTodoTasks() {
    const tasks = this.getAll()
    return tasks.filter(task => task.status === 'todo')
  }
  
  // 获取进行中任务
  getDoingTasks() {
    const tasks = this.getAll()
    return tasks.filter(task => task.status === 'doing')
  }
  
  // 获取已完成任务
  getDoneTasks() {
    const tasks = this.getAll()
    return tasks.filter(task => task.status === 'done')
  }
} 